package com.fafa.pos.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import com.fafa.pos.R;
import com.fafa.pos.dao.DBUtil;
import com.fafa.pos.dao.IConfigDao;
import com.fafa.pos.dao.impl.ConfigDaoImpl;
import com.fafa.pos.pojo.Config;
import com.fafa.pos.util.MyActivityManager;

public class SettingActivity extends TitleActivity {

    //dao相关
    private DBUtil dbUtil;
    private IConfigDao configDao;

    //view相关
    private RelativeLayout serverIp;
    private RelativeLayout webService;
    private RelativeLayout posCode;
    private RelativeLayout scanIntervalTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);


        //初始化标题栏
        setTitle("系统配置");
        showBackwardView(R.string.text_back, true);
        showForwardView(R.string.text_back, false);

        //设置状态栏和标题栏同色
        Window window = this.getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(getResources().getColor(R.color.LoginBlue));

        ViewGroup mContentView = (ViewGroup) this.findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }
        //加入activity管理
        MyActivityManager.getInstance().addActivity(this);

        initDB();
        initView();
        initEvent();
    }

    private void initDB() {
        dbUtil = new DBUtil(getApplicationContext().getFilesDir().getAbsoluteFile()+"/pos.db");
        configDao = new ConfigDaoImpl(dbUtil);
    }

    private void initView() {
        serverIp = (RelativeLayout) findViewById(R.id.serverIp);
        webService = (RelativeLayout) findViewById(R.id.webService);
        posCode = (RelativeLayout) findViewById(R.id.posCode);
        scanIntervalTime = (RelativeLayout) findViewById(R.id.scanIntervalTime);
    }

    private void initEvent() {

        serverIp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                final EditText et = new EditText(SettingActivity.this);
                et.setInputType(InputType.TYPE_CLASS_TEXT);

                final Config config = configDao.getConfig();

                et.setText(config.getProperties().get("serverIp"));

                et.setSelection(et.getText().toString().length());
                builder.setTitle("设置服务器地址").setView(et).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        config.getProperties().put("serverIp",et.getText().toString());
                        configDao.saveConfig(config);

                    }
                }).setNegativeButton("取消",null);
                builder.show();
            }
        });
        webService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                final EditText et = new EditText(SettingActivity.this);
                et.setInputType(InputType.TYPE_CLASS_TEXT);

                final Config config = configDao.getConfig();

                et.setText(config.getProperties().get("webService"));

                et.setSelection(et.getText().toString().length());
                builder.setTitle("配置webService").setView(et).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        config.getProperties().put("webService",et.getText().toString());
                        configDao.saveConfig(config);

                    }
                }).setNegativeButton("取消",null);
                builder.show();
            }
        });
        posCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                final EditText et = new EditText(SettingActivity.this);
                et.setInputType(InputType.TYPE_CLASS_TEXT);
                final Config config = configDao.getConfig();
                et.setText(config.getProperties().get("posCode"));
                et.setSelection(et.getText().toString().length());
                builder.setTitle("设置POS机号").setView(et).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        config.getProperties().put("posCode",et.getText().toString());
                        configDao.saveConfig(config);

                    }
                }).setNegativeButton("取消",null);
                builder.show();
            }
        });
        scanIntervalTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                final EditText et = new EditText(SettingActivity.this);
                et.setInputType(InputType.TYPE_CLASS_NUMBER);
                final Config config = configDao.getConfig();
                et.setText(config.getProperties().get("scanIntervalTime"));
                et.setSelection(et.getText().toString().length());
                builder.setTitle("设置离线模式下同一张卡刷卡的最短间隔时间(s)").setView(et).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        config.getProperties().put("scanIntervalTime",et.getText().toString());
                        configDao.saveConfig(config);
                    }
                }).setNegativeButton("取消",null);
                builder.show();
            }
        });

    }

}
